<?php
namespace app\models;

use fastphp\base\Model;

class Record extends Model
{
    protected $table = 'record';
    public function list($page, $limit, $desc=false)
    {
        $limits = ['display=?'];
        $vars = ['1'];
        if (@$_GET['problem']) {
            array_push($limits, 'problem=?');
            array_push($vars, @$_GET['problem']);
        }
        $recordList = (new Record())->where($limits, $vars)->order(['`submitid`+0 ' . ($desc ? 'DESC' : 'ASC')])->limit([($page-1)*$limit . ',' .$limit])->fetchAll();
        foreach ($recordList as &$record) {
            $record['user_info'] = (new User())->getById($record['user']);
            $record['problem_info'] = (new Problem())->get($record['problem']);
        }
        return $recordList;
    }
    public function get($id)
    {
        $record = (new Record())->where(['submitid=?', 'display=1'], [$id])->fetch();
        if (!$record) return false;
        $decoded = base64_decode($record['code']);
        $record['code'] = htmlspecialchars($decoded===false?$record['code']:$decoded);
        $record['language_class'] = StringFunction::getLanguageClass($record['language']);
        $record['info'] = json_decode($record['info'], true);
        return $record;
    }
    public static function getRecords($pid)
    {
        $limits = ['display=?', 'problem=?'];
        $vars = ['1', $pid];
        $records = sizeof((new Record())->where($limits, $vars)->fetchAll());
        return $records;
    }
    public static function getRecordsAC($pid)
    {
        $limits = ['display=?', 'problem=?', 'status=?'];
        $vars = ['1', $pid, '1'];
        $records = sizeof((new Record())->where($limits, $vars)->fetchAll());
        return $records;
    }
    public static function getStatusColor($status, $size = "20")
    {
        $return = "";
        switch ($status) {
            case "-1":
                $return = '<div class="record"><div class="waiting"><i class="fa fa-spinner fa-spin fa-fw"></i><span>Waiting</span></div></div>';
                break;
            case "8":
                $return = '<div class="record"><div class="judging"><i class="fa fa-spinner fa-spin fa-fw"></i><span>Judging</span></div></div>';
                break;
            case "0":
                $return = '<div class="record"><div class="unknownError"><i class="fa fa-question-circle-o fa-fw"></i><span>Unknown Error</span></div></div>';
                break;
            case "1":
                $return = '<div class="record"><div class="accepted"><i class="fa fa-check fa-fw"></i><span>Accepted</span></div></div>';
                break;
            case "2":
                $return = '<div class="record"><div class="wrongAnswer"><i class="fa fa-close fa-fw"></i><span>Wrong Answer</span></div></div>';
                break;
            case "3":
                $return = '<div class="record"><div class="runtimeError"><i class="fa fa-times-circle fa-fw"></i><span>Runtime Error</span></div></div>';
                break;
            case "4":
                $return = '<div class="record"><div class="compileError"><i class="fa fa-exclamation-circle fa-fw"></i><span>Compile Error</span></div></div>';
                break;
            case "5":
                $return = '<div class="record"><div class="timeLimitExceeded"><i class="fa fa-clock-o fa-fw"></i><span>Time Limit Exceeded</span></div></div>';
                break;
            case "6":
                $return = '<div class="record"><div class="memoryLimitExceeded"><i class="fa fa-microchip fa-fw"></i><span>Memory Limit Exceeded</span></div></div>';
                break;
            case "7":
                $return = '<div class="record"><div class="outputLimitExceeded"><i class="fa fa-terminal fa-fw"></i><span>Output Limit Exceeded</span></div></div>';
                break;
        }
        return $return;
    }
}